package com.fuhe.chen.vendingmachine.service.impl;

import com.fuhe.chen.vendingmachine.common.redis.RedisUtils;
import com.fuhe.chen.vendingmachine.dao.CommoditySellLabelDao;
import com.fuhe.chen.vendingmachine.pojo.Commodity;
import com.fuhe.chen.vendingmachine.pojo.CommoditySellLabel;
import com.fuhe.chen.vendingmachine.pojo.RelationshipCommodityLabel;
import com.fuhe.chen.vendingmachine.service.ICommoditySellLabelService;
import com.fuhe.chen.vendingmachine.service.ICommodityService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.fuhe.chen.vendingmachine.constant.CacheConstant.COMMODITY_LABEL;
import static com.fuhe.chen.vendingmachine.constant.CacheConstant.COMMODITY_LABEL_RELATIONSHIP;

@Service
public class CommoditySellLabelServiceImpl implements ICommoditySellLabelService {

    @Autowired
    CommoditySellLabelDao commoditySellLabelDao;

    @Autowired
    ICommodityService commodityService;

    @Autowired
    RedisUtils redisUtils;

    @Override
    @Transactional
    public String addLabel(CommoditySellLabel label) {
        List<CommoditySellLabel> all = findAll();
        for (CommoditySellLabel commoditySellLabel : all) {
            if(commoditySellLabel.getTitle().equals(label.getTitle())){
                return "标签已存在";
            }
        }
        commoditySellLabelDao.addLabel(label);

        //数据库变动,清除缓存
        redisUtils.delAll(COMMODITY_LABEL);
        redisUtils.delAll(COMMODITY_LABEL_RELATIONSHIP);

        return "添加成功";
    }

    @Override
    @Transactional
    public void deleteLabel(int id) {

        commoditySellLabelDao.deleteLabel(id);

        //数据库变动,清除缓存
        redisUtils.delAll(COMMODITY_LABEL);
        redisUtils.delAll(COMMODITY_LABEL_RELATIONSHIP);
    }

    @Override
    @Transactional
    public String bindCommodity(Integer commodityId, Integer labelId) {
        List<RelationshipCommodityLabel> allRelationship = findAllRelationship();
        for (RelationshipCommodityLabel relationship : allRelationship) {
            if(relationship.getCommodity_id() == commodityId && relationship.getLabel_id() == labelId){
                return "商品已绑定,此为重复操作";
            }
        }
        commoditySellLabelDao.addBindRelationship(commodityId,labelId);
        redisUtils.delAll(COMMODITY_LABEL);
        redisUtils.delAll(COMMODITY_LABEL_RELATIONSHIP);
        return "绑定成功";
    }

    @Override
    @Transactional
    public Integer unBindCommodity(Integer commodityId, Integer labelId) {
        Integer resultRow = commoditySellLabelDao.delBindRelationship(commodityId, labelId);
        redisUtils.delAll(COMMODITY_LABEL);
        redisUtils.delAll(COMMODITY_LABEL_RELATIONSHIP);

        return resultRow;
    }

    @Override
    public List<CommoditySellLabel> findAll() {
        if(redisUtils.hasKey(COMMODITY_LABEL)){
            return (List<CommoditySellLabel>)redisUtils.get(COMMODITY_LABEL);
        }else{
            List<CommoditySellLabel> list = commoditySellLabelDao.findAll();

            //首次查询时,放入缓存中
            redisUtils.set(COMMODITY_LABEL,list);
            return list;
        }
    }

    @Override
    public List<RelationshipCommodityLabel> findAllRelationship() {
        if(redisUtils.hasKey(COMMODITY_LABEL_RELATIONSHIP)){
            return (List<RelationshipCommodityLabel>)redisUtils.get(COMMODITY_LABEL_RELATIONSHIP);
        }else{
            List<RelationshipCommodityLabel> list = commoditySellLabelDao.findAllRelationship();
            redisUtils.set(COMMODITY_LABEL_RELATIONSHIP,list);
            return list;
        }
    }

    @Override
    public PageInfo<Commodity> findCommodityPageByLabel(Integer labelId,int pageNum,int pageSize) {
        PageInfo<Commodity> relationshipByLabel = commodityService.findCommodityPageByLabel(labelId, pageNum, pageSize);
        return relationshipByLabel;
    }
}
